#include "car_power.hpp"
#include "csv_struct.hpp"
#include "car.hpp"
#include "file_list.hpp"



car_power_csv::car_power_csv(): file(file_list::car_power_filename())
{}


bool car_power_csv::exist(int _id)
{
	return static_cast<car_power_struct_csv>(find(_id)).id();
}

float car_power_csv::power(int _id)
{	
	float p(static_cast<car_power_struct_csv>(find(_id)).power());
	
	std::cout << "[car_power::power] car power (cc) = " << p << std::endl;

	return p;
}

void car_power_csv::find_range(float _power, int _power_range[])
{
	csv_struct csv_file(file_list::car_power_filename());
	float power[] = {0., 0.};

	while(_power && !csv_file.eof())
	{
		++csv_file;
		car_power_struct_csv row(csv_file.get_line());

		if(row.power() >= _power && (row.power() < power[1] || !power[1]))
		{
			_power_range[1] = row.id();
			power[1] = row.power();
			
		}

		if(row.power() <= _power && (!power[0] || power[0] > row.power()))
		{
			_power_range[0] = row.id();
			power[0] = row.power();
		}
	}
	
	std::cout << "[car_power_csv::find_range] power range = { " << _power_range[0] <<", " <<_power_range[1] << " ]" << std::endl;
		
}

car_power_struct_csv const car_power_csv::find(int _id)
{	
	csv_struct csv_file(file_list::car_power_filename());

	while(_id && !csv_file.eof())
	{
		++csv_file;
		car_power_struct_csv row(csv_file.get_line());
			
		if(row.id() != _id) continue;	
		
		return row;
	}
	
	car_power_struct_csv car_power_struct_null;
	return car_power_struct_null;
}
